﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;


public partial class ContractList : System.Web.UI.Page
{
    #region Fields
    private const int PageSize = 20;
    #endregion
    #region Properties
    public string ContractID { get; set; }
    public DateTime StartDate { get; set; }
    public string SortField { get; set; }
    public string SortOption { get; set; }
    public int PageIndex
    {
        get
        {
            if (Request["page"] != null)
            {
                return int.Parse(Request["page"].ToString());
            }
            return 1;
        }
    } 
    #endregion
    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Search();
        }
    }
    protected void btnFind_Click(object sender, EventArgs e)
    {
        GetPropertyFromForm();
        Response.Redirect(string.Format(@"/Pages/Agents/ContractList.aspx?ContractID={0}&StartDate={1}&SortField={2}&SortOption={3}", ContractID, StartDate, SortField, SortOption));
    } 
    #endregion
    #region Methods
    protected void GetPropertyFromQueryString()
    {
        if (Request["ContractID"] != null)
            ContractID = Request["ContractID"].ToString();
        else
            ContractID = string.Empty;
        txtContractID.Text = ContractID;
        if (Request["StartDate"] != null)
            StartDate = Convert.ToDateTime(Request["StartDate"].ToString());
        else
            StartDate = new DateTime(2010, 1, 1);
        txtStartDate.Text = StartDate.Date.ToString("dd/MM/yyyy");
    }
    protected void GetPropertyFromForm()
    {
        ContractID = txtContractID.Text;
        StartDate = Convert.ToDateTime(txtStartDate.Text);
    }
    protected void Search()
    {
        GetPropertyFromQueryString();

        HashSet<string> hs = new HashSet<string>();
        //hs.Add(SortField + " " + SortOption);
        int count = 0;
        grv.DataSource = ContractDAO.CreateInstant().SelectListItem(ContractID, StartDate, PageIndex, PageSize, out count, hs);
        grv.DataBind();
        smartPager.PageSize = PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = count;
        smartPager.UrlFormatString = string.Format(@"/Pages/Agents/ContracttList.aspx?ContractID={0}&StartDate={1}&SortField={2}&SortOption={3}", ContractID, StartDate, SortField, SortOption) + "&page={0}";
        smartPager.Visible = count > PageSize;
    }
    protected string GetAgentName(object AgentId)
    {
        string result = string.Empty;
        Guest item = GuestDAO.CreateInstant().GetGuestById(AgentId.ToString());
        if (item != null)
            result = item.FirstName;
        return result;
    } 
    #endregion
}
